home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 445_01 / pi5ways / pindrop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-05  |  2.9 KB  |  90 lines

  1. /**************************************************************************/
  2. /*         Calculate π by  dropping a pin on a wood plank floor.          */      
  3. /*       If any part of the pin touches the "crack" between boards,       */
  4. /*                            it is a HIT.                                */
  5. /*    π is calculated from the ratio of hits to total drops of the pin.   */
  6. /*                                                                        */
  7. /*                               M\Cooper                                 */
  8. /*                       3425 Chestnut Ridge Rd.                          */
  9. /*                        Grantsville, MD 21536                           */
  10. /*                       -------------------------                        */
  11. /*                       email: thegrendel@aol.com                        */   
  12. /*                                                                        */
  13. /*                                06/91                                   */
  14. /*             Source code placed in the public domain.                   */
  15. /**************************************************************************/
  16.  
  17.  
  18.  
  19.  
  20. #include <math.h>
  21. #include <stdlib.h>
  22. #include <conio.h>
  23. #include <stdio.h>
  24.  
  25. #define MAX 1000000
  26. #define MAX_RND 32765
  27. #define MULTIPLIER 10.0
  28.  
  29. double getrand (int );
  30.  
  31. /**********************************************************************/
  32. /*                          GETRAND(n)                                */
  33. /*                Outputs random number (0.0 - n)                     */
  34. /**********************************************************************/
  35.  
  36. double getrand( int upper_lim )
  37.  
  38. {
  39.    double scale_factor,   /*scaling factor*/
  40.           rndnum,
  41.           rand0_1,    /*random number between 0.0 and 1.0*/
  42.           random_number;
  43.  
  44.       scale_factor = (double)MAX_RND;
  45.       rndnum = (double)random( MAX_RND );
  46.  
  47.       rand0_1 = rndnum / scale_factor;
  48.       random_number = rand0_1 * (double)upper_lim;
  49.  
  50.       return( random_number );
  51. }
  52.  
  53. /**************************************************************************/
  54.  
  55.  
  56. void main()
  57. {
  58.    double Dx,
  59.           theta,
  60.           ratio,
  61.           Pi;
  62.    long hits = 0,
  63.         drops = 0;
  64.  
  65.       randomize();
  66.       clrscr();  
  67.  
  68.  
  69.       while (drops < MAX)
  70.          {
  71.          drops++;
  72.          Dx =  getrand( 1 );   /*Generate horizontal position of one end of the pin*/
  73.          theta = getrand( 180 );   /* Angle of the pin from the horizontal */
  74.  
  75.          if( theta <= 90.0 )
  76.             if( Dx + cos( theta ) >= 1.0 )
  77.                hits++;
  78.  
  79.          if ( theta > 90.0 )
  80.             if( ( Dx - cos( 180.0 - theta ) ) <= 0.0 )
  81.                hits++;
  82.  
  83.          ratio = (double)hits / (double) drops;
  84.  
  85.          printf( "Drops = %5ld   ---   Hits = %5ld               ============>  π ≈ %f \n",
  86.                   drops, hits, MULTIPLIER * ratio);
  87.          }
  88.  
  89. }
  90.